home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.10 Oct 95 / Tip Correction for August < prev   
Encoding:
Text File  |  1995-09-27  |  2.6 KB  |  86 lines  |  [TEXT/ttxt]

  1. Errata - a correction to an old tip of the month.
  2. The source for Luigi Belverato’s August Tip of the Month, which changed the status of a patch via the Gestalt Manager, was printed incorrectly.  The correct sources are at all the usual places, and on the source diskette.  To correct the sources by hand, replace all occurences of “= 3D” (that’s equal sign, space, three, capital D) and “=3D” (without a space) with “=” (no spaces).
  3.  
  4.  
  5.  
  6.  
  7.  
  8. While developing an init that installs a system wide patch I needed a simple way to enable or disable it (forcing it to call the original patch) from the controlling application.
  9. I found that the Gestalt manager could come in useful. In fact the procedure that installs a new selector, used in this case to check for the existence of the init, requires you to specify a response function which gets executed whenever the new selector is called. Being the function itself is part of the init, it is able to change the value of a status variable and return its state: assuming that the variable is a boolean.  With two gestalt calls (max) it is possible to change the patch status.  Follow some code fragments:
  10.  
  11. // global constants
  12. #define    kPatchSelector        'Test'            // must be OSType
  13. #define    kPatchNotPresent    0
  14. #define    kPatchPresent            1
  15.  
  16. // INIT FRAGMENT
  17. // INIT globals
  18. Boolean                            gUsePatch =false;
  19. SelectorFunctionUPP        gGestaltSelectorFunction;
  20.  
  21. // install new gestalt selector
  22. OSErr InstallGestalt(void) 
  23. {
  24.     OSErr    error;
  25.  
  26.     long         oldA4;
  27.     oldA4 = SetUpA4();
  28.  
  29.     gGestaltSelectorFunction = 
  30.         3DNewSelectorFunctionProc(PatchGestalt);
  31.     error =NewGestalt(kPatchSelector, 
  32.         gGestaltSelectorFunction);
  33.  
  34.     RestoreA4(oldA4);
  35.     return error;
  36. }
  37.  
  38. //gestalt response function
  39. pascal OSErr PatchGestalt(OSType gestaltSelector,long* 
  40.     gestaltResponse)
  41. {
  42.     // use the global data in _this file_
  43.     long oldA4;
  44.     oldA4 = SetUpA4();
  45.  
  46.     if (gUsePatch) { 
  47.         *gestaltResponse =kPatchNotPresent;
  48.         gUsePatch =false;
  49.     }
  50.     else {
  51.         *gestaltResponse =kPatchPresent;
  52.         gUsePatch=true;
  53.     }
  54.  
  55.     // restore the a4 world
  56.     RestoreA4(oldA4);
  57.     
  58.     return noErr;
  59. }
  60. // APPLICATION FRAGMENT
  61. OSErr TogglePatch(Boolean engage)
  62. {
  63.     OSErr    error=noErr;
  64.     long        gestaltFeature=0;
  65.  
  66.     //this call switches the = patch status
  67.     error =Gestalt(kPatchSelector,&gestaltFeature);
  68.  
  69.     if (engage) {
  70.         // was = already engaged; turn it back on
  71.         if ((error = =noErr) &&         
  72.                 (gestaltFeature = =kPatchNotPresent))
  73.         error =Gestalt(kPatchSelector,&gestaltFeature);
  74.     }
  75.     else    {
  76.         // was = already disabled; turn it back off
  77.         if ((error==noErr) && 
  78.             (gestaltFeature==kPatchPresent))    
  79.         error=Gestalt(kPatchSelector,&gestaltFeature);
  80.     }
  81.  
  82.     return error;
  83. }
  84. – Luigi Belverato
  85. Milano, Italy
  86.